package com.wnxy.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.admin.common.handler.Asserts;
import com.wnxy.admin.common.handler.BusinessEnum;
import com.wnxy.admin.dto.AlarmMessageDto;
import com.wnxy.admin.dto.PageResult;
import com.wnxy.admin.entity.AlarmMessages;
import com.wnxy.admin.entity.MaintenanceTasks;
import com.wnxy.admin.mapper.AlarmMessagesMapper;
import com.wnxy.admin.mapper.MaintenanceTasksMapper;
import com.wnxy.admin.service.IAlarmMessagesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.admin.vo.AlertEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
@RequiredArgsConstructor
public class AlarmMessagesServiceImpl extends ServiceImpl<AlarmMessagesMapper, AlarmMessages> implements IAlarmMessagesService {

    private final AlarmMessagesMapper alarmMessagesMapper;
    private final MaintenanceTasksMapper maintenanceTasksMapper;

    @Override
    public PageResult queryAlarmMessage(AlarmMessageDto alarmMessageDto) {
        Page page = Page.of(alarmMessageDto.getPageNum(), alarmMessageDto.getPageSize());

        //根据充电桩/充电站/报警类型/警报详情 进行匹配
        Page result = alarmMessagesMapper.selectPage(page, Wrappers.lambdaQuery(AlarmMessages.class)
                .like(!StringUtils.isEmpty(alarmMessageDto.getChargerName()), AlarmMessages::getChargerName, alarmMessageDto.getChargerName())
                .like(!StringUtils.isEmpty(alarmMessageDto.getStationName()), AlarmMessages::getStationName, alarmMessageDto.getStationName())
                .like(!StringUtils.isEmpty(alarmMessageDto.getAlarmType()), AlarmMessages::getAlarmType, alarmMessageDto.getAlarmType())
                .like(!StringUtils.isEmpty(alarmMessageDto.getAlarmDetails()), AlarmMessages::getAlarmDetails, alarmMessageDto.getAlarmDetails())
                .eq(AlarmMessages::getFlag, 0));

        return new PageResult<>(result.getTotal(), result.getRecords());
    }

    @Override
    @Transactional
    public void startTask(AlarmMessages alarmMessages) {
        //状态为已处理的消息无法发布任务
        Asserts.fail(alarmMessages.getStatus() == 1, BusinessEnum.MISSION_ACCOMPLISHED);

        //只能发布一般任务
        Asserts.fail(alarmMessages.getLevelDegree() != 1, BusinessEnum.MISSION_LEVEL_DISCREPANCY);

        //根据消息id查询任务
        MaintenanceTasks dbTask = maintenanceTasksMapper.selectOne(Wrappers.lambdaQuery(MaintenanceTasks.class)
                .eq(MaintenanceTasks::getTaskAlarmId, alarmMessages.getMessageId()));

        //判断任务是否已发布
        Asserts.fail(!ObjectUtils.isEmpty(dbTask), BusinessEnum.TASK_HAS_BEEN_RELEASED);

        //构建一般任务对象
        MaintenanceTasks maintenanceTasks = new MaintenanceTasks();

        maintenanceTasks.setTaskAlarmId(alarmMessages.getMessageId());
        maintenanceTasks.setTaskName(alarmMessages.getChargerName() + AlertEnum.Maintenance_tasks.getMsg());
        maintenanceTasks.setTaskType(AlertEnum.Maintenance_tasks.getMsg());

        //手动发布任务直接将数据存入任务管理，由定时器扫描并分配任务
        maintenanceTasksMapper.insert(maintenanceTasks);
    }

    @Override
    @Transactional
    public void cancelTask(AlarmMessages alarmMessages) {
        //(根据消息id)查询任务
        MaintenanceTasks maintenanceTasks = maintenanceTasksMapper.selectById(alarmMessages.getMessageId());

        //判断是否为空，为空则任务未发布
        Asserts.fail(!ObjectUtils.isEmpty(maintenanceTasks), BusinessEnum.THE_TASK_IS_NOT_PUBLISHED);

        //只能取消一般任务(报警消息等级为1)
        Asserts.fail(alarmMessages.getLevelDegree() != 1, BusinessEnum.RISK_LEVEL_IS_TOO_HIGH);

        //只能取消未处理的任务
        Asserts.fail(maintenanceTasks.getStatus() != 0, BusinessEnum.THE_TASK_HAS_BEEN_PROCESSED);

        //删除任务管理中的对应的任务
        maintenanceTasksMapper.deleteById(maintenanceTasks);

    }
}
